﻿using System;
using DataStore;
using DataStore.Domain;
using DataStore.Repositories;
using Infrastructure.CurrentData;
using Microsoft.Practices.Composite.Events;

namespace Infrastructure.ViewModels
{
    /// <summary>
    /// Author viewmodel
    /// </summary>
    public class AuthorViewModel : PersonViewModel
    {
        Author _author;

        /// <summary>
        /// Initializes a new instance of the <see cref="AuthorViewModel"/> class.
        /// </summary>
        /// <param name="author">The author.</param>
        public AuthorViewModel(Author author) : base(author)
        {
            Initialize(author);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AuthorViewModel"/> class.
        /// </summary>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="author">The author.</param>
        public AuthorViewModel(IEventAggregator eventAggregator, Author author) : base(eventAggregator, author)
        {
            Initialize(author);
        }

        /// <summary>
        /// Initializes the specified author.
        /// </summary>
        /// <param name="author">The author.</param>
        void Initialize(Author author)
        {
            _author = author;
        }

        /// <summary>
        /// Gets the item.
        /// </summary>
        /// <value>The item.</value>
        public new Author Item
        {
            get
            {
                return _author;
            }
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public override Guid Id
        {
            get
            {
                return _author.Id;
            }
            set
            {
                _author.Id = value;
                OnPropertyChanged("Id");
            }
        }
        /// <summary>
        /// Gets or sets the full name of the person.
        /// </summary>
        /// <value>The full name.</value>
        public override string Name
        {
            get
            {
                return string.Format("{0} {1} {2}", Title, FirstName, LastName);
            }
        }
        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        /// <value>The first name.</value>
        public override string FirstName
        {
            get
            {
                return _author.FirstName;
            }
            set
            {
                _author.FirstName = value;
                OnPropertyChanged("FirstName");
            }
        }
        /// <summary>
        /// Gets or sets the name of the middle.
        /// </summary>
        /// <value>The name of the middle.</value>
        public override string MiddleName
        {
            get
            {
                return _author.MiddleName;
            }
            set
            {
                _author.MiddleName = value;
                OnPropertyChanged("MiddleName");
            }
        }
        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        /// <value>The last name.</value>
        public override string LastName
        {
            get
            {
                return _author.LastName;
            }
            set
            {
                _author.LastName = value;
                OnPropertyChanged("LastName");
            }
        }

        /// <summary>
        /// Gets or sets the employment position, job.
        /// </summary>
        /// <value>The position.</value>
        public override string Position
        {
            get
            {
                return _author.Position;
            }
            set
            {
                _author.Position = value;
                OnPropertyChanged("Position");
            }
        }
        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public override string Title
        {
            get
            {
                return _author.Title;
            }
            set
            {
                _author.Title = value;
                OnPropertyChanged("Title");
            }
        }

        /// <summary>
        /// Gets a value indicating whether thye entity is a new item.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the entity is a new item; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsNewItem
        {
            get
            {
                return _author.Id == Guid.Empty;
            }
        }

        /// <summary>
        /// Saves the entity.
        /// </summary>
        public override void Save()
        {
            IDatabase database = Current.Container.Resolve<IDatabase>();
            using (var unitOfWork = database.CreateUnitOfWork())
            {

                try
                {
                    IRepository<Author> repository = new AuthorRepository(unitOfWork);
                    unitOfWork.BeginTransaction();
                    repository.SaveOrUpdate(_author);
                    unitOfWork.CommitTransaction();
                }
                catch
                {
                    unitOfWork.RollbackTransaction();
                    throw;
                }
            }

            OnUpdated(this, EventArgs.Empty);
        }

        /// <summary>
        /// Gets a value indicating whether this instance can save.
        /// </summary>
        /// <value><c>true</c> if this instance can save; otherwise, <c>false</c>.</value>
        protected override bool CanSave
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Gets the author.
        /// </summary>
        /// <value>The author.</value>
        public Author Author
        {
            get
            {
                return _author;
            }
        }
    }
}